// components/work-location/index.js
import {$wuxDialog} from '../../wux-components/index'
import { getDistance, openMap} from '../../utils/common.js'
const T = getApp().globalData.T
Component({
  /**
   * 组件的属性列表
   * @param address 公司地址
   * @param ln 经纬度
   * @param isShowLn 是否显示经纬度
   */
  properties: {
    address: {
      type: String,
      value: 'xxx'
    },
    ln: {
      type: String,
      value: ''
    },
    isShowLn: {
      type: Boolean,
      value: false
    }
  },

  observers: {
    'ln': function(ln) {
      if (!ln) return false;
      let lat = Number(this.properties.ln.split(',')[1]);
      let long = Number(this.properties.ln.split(',')[0]);
      (function _d(that){
        wx.getLocation({
          type: 'wgs84',
          success: (res) => {
            console.log(res.latitude, res.longitude, ';',lat, long)
            console.log('距离:' + getDistance(res.latitude, res.longitude, lat, long));
            that.setData({
              distance: parseInt(getDistance(res.latitude, res.longitude, lat, long)) || 0
            });
          },
          fail: function (fail) { 
            console.log(fail);
            $wuxDialog().confirm({
              resetOnClose: true,
              closable: true,
              title: '设置权限',
              content: '为了更好地服务您,请开启位置授权',
              onConfirm(e) {
                wx.openSetting({
                  success: auth => {
                    console.log(auth);
                    if (auth.authSetting['scope.userLocation']) {
                      _d(that);
                    }
                  },
                  fail: function (fail) {
                    console.log(fail);
                  }
                })
              },
              onCancel(e) {
                console.log('取消授权')
              },
            })
          }
        })
      })(this)
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    distance: 'x'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onJumpMap() {
      let param = {
        address: this.properties.address,
        lt: Number(this.properties.ln.split(',')[0]),
        la: Number(this.properties.ln.split(',')[1]),
      }
      openMap(param);
    }
  }

})